﻿using EasyCore.App;
using EasyCore.Utils.Cryptogram;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.Extensions
{
    public static class EncryptionExtensions
    {
        #region SM2加密与解密
        /// <summary>
        /// 对字符串采用SM2加密，密钥由配置文件提供
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public static string SM2Encrypt(this string msg)
        {
            string publicKey = EasyApp.Configuration.Get<string>("Cryptogram:SM2:PublicKey");
            if (string.IsNullOrWhiteSpace(publicKey)) throw new Exception("SM2公钥在配置文件中未配置");
            string str = SM2Util.Encrypt(msg, publicKey);
            return str;
        }

        /// <summary>
        /// 对字符串采用SM2进行解密，密钥由配置文件提供
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string SM2Decrypt(this string msg)
        {
            string privateKey = EasyApp.Configuration.Get<string>("Cryptogram:SM2:PrivateKey");
            if (string.IsNullOrWhiteSpace(privateKey)) throw new Exception("SM2私钥在配置文件中未配置");
            string str = SM2Util.Decrypt(msg, privateKey);
            return str;
        }
        #endregion 加密与解密


        #region AES加密与解密
        public static string AESEncrypt(string plainText)
        {
            string aesKey = EasyApp.Configuration.Get<string>("Cryptogram:AES:AesKey");
            string aesIV = EasyApp.Configuration.Get<string>("Cryptogram:AES:AesIV");
            using (Aes aes = Aes.Create())
            {
                aes.Key = Convert.FromBase64String(aesKey);
                aes.IV = Convert.FromBase64String(aesIV);
                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(cs))
                        {
                            sw.Write(plainText);
                        }
                    }
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
        }

        public static string AESDecrypt(string cipherText)
        {
            string aesKey = EasyApp.Configuration.Get<string>("Cryptogram:AES:AesKey");
            string aesIV = EasyApp.Configuration.Get<string>("Cryptogram:AES:AesIV");
            using (Aes aes = Aes.Create())
            {
                aes.Key = Convert.FromBase64String(aesKey);
                aes.IV = Convert.FromBase64String(aesIV);
                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                byte[] bytes = Convert.FromBase64String(cipherText);

                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(cs))
                        {
                            return sr.ReadToEnd();
                        }
                    }
                }
            }
        }
        #endregion AES加密与解密


        #region DES加密与解密
        public static string DESEncrypt(string plainText)
        {
            string aesKey = EasyApp.Configuration.Get<string>("Cryptogram:AES:AesKey");
            string aesIV = EasyApp.Configuration.Get<string>("Cryptogram:AES:AesIV");
            using (DES aes = DES.Create())
            {

                aes.Key = Convert.FromBase64String(aesKey);
                aes.IV = Convert.FromBase64String(aesIV);

                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(cs))
                        {
                            sw.Write(plainText);
                        }
                    }

                    return Convert.ToBase64String(ms.ToArray());
                }
            }
        }

        public static string DESDecrypt(string cipherText)
        {
            string aesKey = EasyApp.Configuration.Get<string>("Cryptogram:AES:AesKey");
            string aesIV = EasyApp.Configuration.Get<string>("Cryptogram:AES:AesIV");
            using (DES aes = DES.Create())
            {

                aes.Key = Convert.FromBase64String(aesKey);
                aes.IV = Convert.FromBase64String(aesIV);

                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                byte[] bytes = Convert.FromBase64String(cipherText);

                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(cs))
                        {
                            return sr.ReadToEnd();
                        }
                    }
                }
            }
        }
        #endregion DES加密与解密
    }
}
